package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.UpdateNickOrSignTask;
import com.xgkj.diyiketang.weidget.ClearableEditText;


public class UpdateNickNameActivity extends BaseActivity {
    private Context mContext;
    private TextView submitBtn;
    private ClearableEditText nickNameEdit;
    private EatShowUserInfo userInfo;
    private String nickName;
    private String area;
    private int updateUserInfoTaskId = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.update_nick_name);
        mContext = this;
        initView();

    }

    private void initView() {
        submitBtn = (TextView) findViewById(R.id.save);
        nickNameEdit = (ClearableEditText) findViewById(R.id.nickname_edit);
        userInfo = getEatShowApp().getmUserInfo();
        if(userInfo != null){
            String nick = userInfo.getNickName();
            if(nick != null && !nick.equals("")) {
                nickNameEdit.setText(nick);
                nickNameEdit.setSelection(nick.length());
            }
        }
        submitBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(checkInput()){
                    updateUserInfo();
                }
            }
        });
        ((TextView)findViewById(R.id.back)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private boolean checkInput(){
        nickName = nickNameEdit.getText().toString();
        if(nickName == null || (nickName.trim()).equals("")){
            Toast.makeText(mContext,R.string.nickname_not_null,Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    private void updateUserInfo(){
        if(updateUserInfoTaskId != -1){
            return;
        }
        UpdateNickOrSignTask task = new UpdateNickOrSignTask(userInfo.getUserSign(),nickName,true);
        task.addListener(updateUserInfoListener);
        updateUserInfoTaskId = getEatShowApp().getTaskManager().addTask(task);

    }

    private TaskEventListener updateUserInfoListener = new ShowToastTaskEventListener(this){
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            updateUserInfoTaskId = -1;
            getEatShowApp().getmUserInfo().setNickName(nickName);
            Toast.makeText(mContext,R.string.update_nickname_success,Toast.LENGTH_SHORT).show();
            finish();
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
            updateUserInfoTaskId = -1;
        }

        @Override
        public void handleTaskMessage(int taskId, Object data) {
            super.handleTaskMessage(taskId, data);
        }
    };
}
